package com.yuyue.springFramework.note06;

import com.yuyue.springFramework.note06.pk1.*;
import com.yuyue.springFramework.note06.pk2.ConfigEntity6;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * 《Spring-6 @ComponentScan 详解 》文章代码
 *  文章地址：https://mp.weixin.qq.com/s/2a-AIoXEflN9M5bnVIYycg
 */
public class Note06Test {
    @Test
    //@ComponentScan 空参扫描所在类的包及其子包
    public void fun1(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity1.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            System.out.println(applicationContext.getBean(name).getClass());
        }
    }

    @Test
    //@ComponentScan 扫描多个包及其子包
    public void fun2(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity2.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //@ComponentScan 指定扫描的类所在的包及其子包，即使存在相同的类，因所在包的不同，也不会产生冲突
    public void fun3(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity3.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //includeFilters 包含过滤器，未指定 是否启用默认过滤器(useDefaultFilters)
    public void fun4(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity4.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //includeFilters 包含过滤器，指定 是否启用默认过滤器(useDefaultFilters) 为否
    public void fun5(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity5.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //includeFilters 包含过滤器，过滤指定类
    public void fun6(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity6.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //@ComponentScan重复使用,什么结果也取不到
    public void fun7(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity7.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }

    @Test
    //以@ComponentScans重复使用,什么结果也取不到
    public void fun8(){
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(ConfigEntity8.class);
        for (String name : applicationContext.getBeanDefinitionNames()){
            //仅打印自定义的类
            if(applicationContext.getBean(name).getClass().toString().contains("yuyue")){
                System.out.println(applicationContext.getBean(name).getClass());
            }
        }
    }
}
